* calculates heterogeneity contributions and generates figures with sample distributions of estimated structural parameters. The file is set to recover estimates for the main specification used in the paper. Instructions are provided in comments for modifications required for the various robustness checks.

clear
set more off
pause on

cd ""

* created in full_model_simulation.m
insheet using sim_output.csv

* omitted age is 15-16
* sex=female 
rename(v1-v22) (ind_id theta  sig_th r  sig_r kappa int_theta int_kappa int_sig_th int_r int_sig_r sex factor1 factor2 factor3 factor4 female english age17 age18 age19p type)

* Use below for robustness checks which only estimate risk preferences only (e.g. expo-power)
*rename(v1-v18) (ind_id theta  sig_th kappa int_theta int_kappa int_sig_th sex factor1 factor2 factor3 factor4 female english age17 age18 age19p type)


* add below when using robustness check with separate trembles on time tasks
*rename (v23 v24) (kappa_time int_kappa_time)

* for robustness with male sample 
*keep if sex==0

* for robustness with female sample 
* keep if sex==1


sort ind_id


merge m:1 ind_id using base_FE_para_all
drop _merge


******** Converting preference and consistency parameters (Equations 19-23 of the paper) to a form in which they are linear in observed and unobserved heterogeneity

* skip below for robustness where risk parameters only are estimated, e.g. for expo-power utility
foreach var of varlist sig_r  {
	gen inv_`var'=invnormal(`var')
	}

	
* skip below for robustness where risk parameters only are estimated, e.g. for expo-power utility
foreach var of varlist r   {
	gen inv_`var'=invnormal(`var')
	}

/* for robustness with 200% upper limit on discount rates
foreach var of varlist r   {
	gen inv_`var'=invnormal(`var'/2)
	}
*/

foreach var of varlist sig_th {
	gen inv_`var'=invnormal(`var')
	}
	
* uncomment "kappa_time" for robustness with separate mistake parameter for time preferences 
foreach var of varlist kappa /*kappa_time*/ {
	capture gen inv_`var'=invnormal(2*`var')
	}

/*	for robustness with CARA utility
foreach var of varlist sig_th {
	gen inv_`var'=invnormal(20*`var')
	}
	*/
	

	
* uncomment "kappa_time" for robustness with separate mistake parameter for time preferences 
* comment out "r sig_r" * for robustness where risk parameters only are estimated, e.g. for expo-power utility
foreach var of varlist sig_th r sig_r kappa /*kappa_time*/ {
	capture gen int_inv_`var'=int_`var'
	}
	


************* Below Code Replicates Main Results for Explanatory Power of Unobserved factors in terms of cross-sectional variation in preference and consistency parameters

* 1 unobserved type in general is marginal and has extreme risk aversion. Identify that type and if necessary replace in code below(by default set to type 1) 
bysort type: sum theta

local i=1
* uncomment "kappa_time" for robustness with separate mistake parameter for time preferences 
* comment out "r sig_r" * for robustness where risk parameters only are estimated, e.g. for expo-power utility
foreach var of varlist theta inv_r inv_sig_th  inv_sig_r /*inv_kappa_time*/ inv_kappa  {
	capture drop r2_pct_`var'
	* Records the share of explained cross-sectional variation in each preference and consistency parameter attributable to observed and unobserved heterogeneity
	gen r2_pct_`var'=.
    local unobs="int_`var'"
	
	*** excludes the unobserved type with extreme risk aversion. Default is set to type 1 being the extreme type but this might change from estimation to estimation as the types are not naturally ordered. Check with code above and adjust in case a different type is marginal and extreme in risk aversion 
	if `i'==1 | `i'==3 {
		local word=" if type!=1"
		}
	else {
		local word=""
		}	
	reg `var' `unobs' sex factor1-factor4 `word'
	replace r2_pct_`var'=`e(r2)' in 1
	local tot=`e(r2)'		
	reg `var' `unobs' `word'
	* adjustment in case shares do not add up to exactly 1
	local run_tot=`e(r2)'	
	reg `var' sex `word'
	local run_tot=`run_tot'+`e(r2)'
	reg `var' factor1 `word'
	local run_tot=`run_tot'+`e(r2)'
	reg `var' factor2 `word'
	local run_tot=`run_tot'+`e(r2)'
	reg `var' factor3 `word'
	local run_tot=`run_tot'+`e(r2)'
	reg `var' factor4 `word'
	local run_tot=`run_tot'+`e(r2)'	
	
	reg `var' `unobs' `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot' in 2
    local ex=(1-`e(r2)'/`run_tot')
	replace r2_pct_`var'=`ex' in 3
	reg `var' sex `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot'/`ex' in 4	
	reg `var' factor1 `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot'/`ex' in 6
	reg `var' factor2 `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot'/`ex' in 7
	reg `var' factor3 `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot'/`ex' in 8
	reg `var' factor4 `word'
	replace r2_pct_`var'=`e(r2)'/`run_tot'/`ex' in 9
	di "`unobs'"
	
	local ++i
	}


* obtains percentage of variation in latent factors attributable to the deterministic part, for Table 6 of the paper 
local i=1
foreach var of varlist factor1-factor4 {
	capture drop r2_`var' r2_pct_`var'
	* Records the R2 of observed characteristics
	gen r2_`var'=.
	reg `var' female english age17 age18 age19p
	replace r2_`var'=`e(r2)' in 1
	local ++i
	}
	
	
	
****************************** Generates figures which overlay distributions of fixed effects and simulated preference and consistency parameters


gen fe_theta=RP_th_t_base
gen fe_sig_th=RP_sig_th_t_base 
gen fe_kappa=RP_K_t_base
gen fe_r=RP_r_t_base 
gen fe_sig_r=RP_sig_r_t_base

graph drop _all

local i=1
foreach var in theta r sig_th sig_r kappa {
	 capture sum `var', detail
	 local med=`r(p50)'
	 capture sum fe_`var', detail	 
	 local med_fe=`r(p50)'
	 di "`med_fe'"
	 di "`med'"
	
	if "`var'"=="theta" {
		local lab="Risk Aversion"
		local scale=1.5
		}
	if "`var'"=="r" {
		local lab="Discount Rate"
		local scale=15
		}
	if "`var'"=="sig_th" {
		local lab="Risk Aversion SD"
		local scale=15
		}
	if "`var'"=="sig_r" {
		local lab="Discount Rate SD"
		local scale=20
		}
	if "`var'"=="kappa" {
		local lab="% Hand Trembles"
		local scale=40
		}		
	twoway hist fe_`var' if fe_`var'>=-1 & fe_`var'<=3, color(green) || hist `var' if `var'>=-1 & `var'<=3, fcolor(none) lcolor(black) || scatteri 0 `med_fe' `scale' `med_fe', lcolor(red) lpattern(dash) c(l) m(i) lwidth(medthick) || scatteri 0 `med' `scale' `med', lcolor(blue) lpattern("-.") c(l) m(i) lwidth(medthick) name(tm`i') legend(label(1 "Individual FE Estimation") label(2 "Full Model Estimation") label(3 "FE Median") label(4 "Full Model Median")) xtitle("`lab'")
	local ++i
	}
	
save graph_data, replace